import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.ensemble import GradientBoostingRegressor
from sklearn.metrics import mean_squared_error

# 1. 数据加载
train_data = pd.read_csv('input/train.csv')
test_data = pd.read_csv('input/test.csv')



# 2. 数据预处理
# 合并训练集和测试集以便进行统一的特征工程
all_data = pd.concat([train_data, test_data], ignore_index=True)

# 处理缺失值
all_data.fillna(0, inplace=True)

# 转换分类变量
all_data = pd.get_dummies(all_data)

# 分离训练集和测试集
train_data = all_data[all_data['Id'].isin(train_data['Id'])]
test_data = all_data[all_data['Id'].isin(test_data['Id'])]

# 特征和目标变量
features = train_data.drop(['Id', 'SalePrice'], axis=1)
target = train_data['SalePrice']

# 3. 对所有数值型特征进行指数平滑处理
# 获取所有数值型列
numeric_cols = features.select_dtypes(include=[np.number]).columns

# 应用指数加权移动平均（Exponential Weighted Moving Average）
# 使用 com=10，可根据实际数据调整
smoothed_features = features[numeric_cols].ewm(com=10).mean()

# 重命名列名（可选）
smoothed_features.columns = [f"{col}_smoothed" for col in numeric_cols]

# 合并平滑后的特征和原始的非数值型特征（如果有）
non_numeric_cols = features.select_dtypes(exclude=[np.number]).columns
features_smoothed = pd.concat([smoothed_features, features[non_numeric_cols]], axis=1)

# 4. 模型训练和评估
# 分割训练集和验证集
X_train, X_val, y_train, y_val = train_test_split(features_smoothed, target, test_size=0.2, random_state=42)

# 初始化模型
model = GradientBoostingRegressor(random_state=42)

# 训练模型
model.fit(X_train, y_train)

# 验证模型
predictions = model.predict(X_val)
mse = mean_squared_error(y_val, predictions)
print(f'Model MSE after exponential smoothing on all numerical features: {mse}')
